package com.zhonghui.wms.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.zhonghui.response.BaseResult;
import com.zhonghui.wms.domain.WmsMaterialInventory;
import com.zhonghui.wms.domain.WmsWarehouseArea;
import com.zhonghui.wms.domain.vo.WmsInventoryDetailsVo;
import com.zhonghui.wms.domain.vo.WmsMaterialInventoryVo;
import com.zhonghui.wms.service.IWmsInventoryDetailsService;
import com.zhonghui.wms.service.IWmsMaterialInventoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zhonghui.common.annotation.Log;
import com.zhonghui.common.core.controller.BaseController;
import com.zhonghui.common.enums.BusinessType;
import com.zhonghui.wms.domain.WmsWarehouseSeat;
import com.zhonghui.wms.service.IWmsWarehouseSeatService;
import com.zhonghui.common.utils.poi.ExcelUtil;
import com.zhonghui.common.core.page.TableDataInfo;

/**
 * 库位Controller
 *
 * @author zhonghui
 * @date 2022-05-27
 */
@Api(tags="智能仓储WMS-库位")
@RestController
@RequestMapping("/wms/seat")
public class WmsWarehouseSeatController extends BaseController
{
    @Autowired
    private IWmsWarehouseSeatService wmsWarehouseSeatService;

    @Autowired
    private IWmsInventoryDetailsService wmsInventoryDetailsService;

    /**
     * 查询库位列表
     */
    @ApiOperation("查询智能仓储WMS-库位列表")
    @GetMapping("/list")
    public TableDataInfo<List<WmsWarehouseSeat>> list(WmsWarehouseSeat wmsWarehouseSeat)
    {
        startPage();
        List<WmsWarehouseSeat> list = wmsWarehouseSeatService.selectWmsWarehouseSeatList(wmsWarehouseSeat);
        return getDataTable(list);
    }
    /**
     * 查询库位列表不分页
     */
    @ApiOperation("查询智能仓储WMS-库位不分页")
    @GetMapping("/seatList")
    public BaseResult<List<WmsWarehouseSeat>> seatList(WmsWarehouseSeat wmsWarehouseSeat)
    {
        return BaseResult.success(wmsWarehouseSeatService.selectWmsWarehouseSeatList(wmsWarehouseSeat));
    }
    /**
     * 导出库位列表
     */
    @ApiOperation("导出智能仓储WMS-库位列表")
    @Log(title = "库位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WmsWarehouseSeat wmsWarehouseSeat)
    {
        List<WmsWarehouseSeat> list = wmsWarehouseSeatService.selectWmsWarehouseSeatList(wmsWarehouseSeat);
        ExcelUtil<WmsWarehouseSeat> util = new ExcelUtil<WmsWarehouseSeat>(WmsWarehouseSeat.class);
        util.exportExcel(response, list, "库位数据");
    }

    /**
     * 获取库位详细信息
     */
    @ApiOperation("获取智能仓储WMS-库位详细信息")
    @GetMapping(value = "/{id}")
    public BaseResult<WmsWarehouseSeat> getInfo(@PathVariable("id") Long id)
    {
        return BaseResult.success(wmsWarehouseSeatService.selectWmsWarehouseSeatById(id));
    }

    /**
     * 新增库位
     */
    @ApiOperation("新增智能仓储WMS-库位")
    @Log(title = "库位", businessType = BusinessType.INSERT)
    @PostMapping
    public BaseResult<Integer> add(@RequestBody WmsWarehouseSeat wmsWarehouseSeat)
    {
        return BaseResult.success(wmsWarehouseSeatService.insertWmsWarehouseSeat(wmsWarehouseSeat));
    }

    /**
     * 修改库位
     */
    @ApiOperation("修改智能仓储WMS-库位")
    @Log(title = "库位", businessType = BusinessType.UPDATE)
    @PutMapping
    public BaseResult<Integer> edit(@RequestBody WmsWarehouseSeat wmsWarehouseSeat)
    {
        return BaseResult.success(wmsWarehouseSeatService.updateWmsWarehouseSeat(wmsWarehouseSeat));
    }

    /**
     * 删除库位
     */
    @ApiOperation("删除智能仓储WMS-库位")
    @Log(title = "库位", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public BaseResult<Integer> remove(@PathVariable Long[] ids)
    {
        for (Long id : ids){
            WmsInventoryDetailsVo wiDetailsVo = new WmsInventoryDetailsVo();
            wiDetailsVo.setWarehouseSeatId(id);
            List<WmsInventoryDetailsVo> wiDetailsVoList = wmsInventoryDetailsService.selectWmsInventoryDetailsList(wiDetailsVo);
            if (wiDetailsVoList.size() > 0){
                return BaseResult.error("当前库区已被使用，删除失败！！！");
            }else {
                wmsWarehouseSeatService.deleteWmsWarehouseSeatById(id);
            }
        }
        return BaseResult.success();
    }
}
